package com.yyq.eduucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyq.edumsm.entity.PhoneCode;
import com.yyq.edumsm.service.PhoneCodeService;
import com.yyq.eduucenter.entity.UcenterMember;
import com.yyq.eduucenter.entity.vo.RegisterVo;
import com.yyq.eduucenter.mapper.UcenterMemberMapper;
import com.yyq.eduucenter.service.UcenterMemberService;
import com.yyq.result.JwtUtils;
import com.yyq.result.MD5;
import com.yyq.servicebase.exceptionhandler.YyqException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author yyq
 * @since 2021-04-27
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private   PhoneCodeService phoneCodeService;


    @Override
    public String login(UcenterMember member) {
        //如果手机号为空
        if(member.getMobile()==null||member.getPassword()==null) throw new YyqException(20001,"手机号或者密码唯恐");

        String mobile=member.getMobile();
        String password=member.getPassword();

        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);

        //查询为空
        if(ucenterMember==null) throw new YyqException(20001,"找不到用户");
        //md5加密后的密码与数据库中的密码不相同
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())) throw new YyqException(20001,"密码错误");
        //判断用户是否禁用
        if(ucenterMember.getIsDisabled()) {
            throw new YyqException(20001,"登录失败");
        }

        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }
    //注册
    @Override
    public boolean register(RegisterVo registerVo) {
        boolean flag=false;
        String nickname=registerVo.getNickname();//昵称
        String code=registerVo.getCode();//验证码
        String mobile=registerVo.getMobile();//手机号
        String password=registerVo.getPassword();//密码
        if(nickname==null||code==null||mobile==null||password==null){
            throw new YyqException(20001,"不能为空");
        }

        //验证码暂时不写
        QueryWrapper<PhoneCode> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        PhoneCode one = phoneCodeService.getOne(wrapper);
        //查询结果为空
        if(one==null) throw new YyqException(20001,"错误01");
        //验证码错误
        if(!one.getCode().equals(code)) throw new YyqException(20001,"验证码错误");

        //查看数据库是否已经有该用户
        QueryWrapper<UcenterMember> wrapper1=new QueryWrapper<>();
        wrapper1.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper1);
        if(ucenterMember!=null) throw new YyqException(20001,"已有该用户");

        //向数据库添加该用户
        UcenterMember member=new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));  //密码加密
        member.setAvatar("https://img0.baidu.com/it/u=3157934702,102369409&fm=11&fmt=auto&gp=0.jpg");
        baseMapper.insert(member);
        //插入成功后删除验证码
        phoneCodeService.removeById(one);
        flag=true;

        return flag;
    }

    @Override
    public UcenterMember getByOpendId(String openid) {
        UcenterMember member=new UcenterMember();
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("openid",openid);
        member= baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public int getCount(String day) {
        int count=baseMapper.getCount(day);
        return count;
    }

    @Override
    public String loginin(UcenterMember member) {
        String mobile=member.getMobile();
        String password=member.getPassword();

        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }
}
